package com.baidu.baidumap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.DrivingRouteOverlay;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;

import java.util.ArrayList;
import java.util.List;

/**\
 * 驾车路线
 */

public class DrivingRouteOverlayDemo extends BaseActivity {
    private RoutePlanSearch routePlanSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        search();

    }

    private void search() {
        routePlanSearch = RoutePlanSearch.newInstance();
        routePlanSearch.setOnGetRoutePlanResultListener( new MyListener());
        DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
        PlanNode from = PlanNode.withLocation(hmPos);//创建起点
        PlanNode to = PlanNode.withLocation(new LatLng(40,116));
        drivingRoutePlanOption.from(from);
        drivingRoutePlanOption.to(to);

        List<PlanNode> nodes  =new ArrayList<>();
        nodes.add(PlanNode.withCityNameAndPlaceName("济南","泉城广场"));
        drivingRoutePlanOption.passBy(nodes);//添加途径地点


        /**
         * ECAR_AVOID_JAM
         驾车策略： 躲避拥堵
         ECAR_DIS_FIRST
         驾乘检索策略常量：最短距离
         ECAR_FEE_FIRST
         驾乘检索策略常量：较少费用
         ECAR_TIME_FIRST
         驾乘检索策略常量：时间优先
         */
        drivingRoutePlanOption.policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_DIS_FIRST );
        routePlanSearch.drivingSearch(drivingRoutePlanOption);

    }
    class MyListener implements OnGetRoutePlanResultListener{


        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {


        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
            if (drivingRouteResult==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==drivingRouteResult.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            DrivingRouteOverlay overlay = new DrivingRouteOverlay(baiduMap);
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(drivingRouteResult.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }

    class MyDrivingOverlay extends DrivingRouteOverlay{

        public MyDrivingOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
    }
}
